package cn.wolfcode.mongo.service.impl;

import cn.wolfcode.mongo.domain.TravelComment;
import cn.wolfcode.mongo.repository.TravelCommentMongoRepository;
import cn.wolfcode.mongo.service.ITravelCommentMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentMongoService implements ITravelCommentMongoService {

    @Autowired
    private TravelCommentMongoRepository travelCommentMongoRepository;

    @Override
    public List<TravelComment> queryByTravelId(Long travelId) {
        return travelCommentMongoRepository.findByTravelId(travelId);
    }

    @Override
    public void save(TravelComment comment) {
        String refId = comment.getRefComment().getId();
        if (StringUtils.hasLength(refId)) {
            Optional<TravelComment> o = travelCommentMongoRepository.findById(refId);
            if (o.isPresent()) {
                comment.setRefComment(o.get());
            }
        }
        travelCommentMongoRepository.save(comment);

    }
}
